﻿using INTEX.DataBase;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Controls;
using INTEX.DataBase.Tools;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class YCAEKAUDIT : DocumentBase
    {
        #region-------------Properties-----------------------
        public ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        decimal RelesedQty
        {
            get
            {
                if (txtReleased.Text.ToString().Trim() == "")
                    txtReleased.Text = "0";
                return Convert.ToDecimal(txtReleased.Text.ToString());
            }
            set
            {
                txtReleased.Text = value.ToString();
            }
        } 
        public YCAEKAUDITData m_Data; 
        public YCAEKAUDITReport m_rpt;
        bool IsSupplierLoded = false;
        public string PrnType = "EXCEL";
        public SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetData");
            }
        }
        public SETINTEXDB Set4ITX
        {
            get
            {
                return (SETINTEXDB)TryFindResource("Set4ITX");
            }
        }

        #region=====vwLIEFDAT=======
        CollectionViewSource vwLIEFDAT
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwLIEFDAT");
            }
        }
        DataRow dwLIEFDAT
        {
            get
            {
                if (vwLIEFDAT.View == null)
                    return null;
                if ((DataRowView)vwLIEFDAT.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwLIEFDAT.View.CurrentItem).Row;
            }
        }
        #endregion

        #region=====vwEKKOPF=======
        CollectionViewSource vwEKKOPF
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwEKKOPF");
            }
        }
        DataRow dwEKKOPF
        {
            get
            {
                if (vwEKKOPF.View == null)
                    return null;
                if ((DataRowView)vwEKKOPF.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwEKKOPF.View.CurrentItem).Row;
            }
        }
        #endregion
        #region=====vwEKPOS=======
        CollectionViewSource vwEKPOS
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwEKPOS");
            }
        }
        DataRow dwEKPOS
        {
            get
            {
                if (vwEKPOS.View == null)
                    return null;
                if ((DataRowView)vwEKPOS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwEKPOS.View.CurrentItem).Row;
            }
        }
        #endregion
        #region=====vwABAUFM=======
        CollectionViewSource vwABAUFM
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwABAUFM");
            }
        }
        DataRow dwABAUFM
        {
            get
            {
                if (vwABAUFM.View == null)
                    return null;
                if ((DataRowView)vwABAUFM.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwABAUFM.View.CurrentItem).Row;
            }
        }
        #endregion
        #region=====vwEvents=======
        CollectionViewSource vwEvents
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwEvents");
            }
        }
        DataRow dwEvents
        {
            get
            {
                if (vwEvents.View == null)
                    return null;
                if ((DataRowView)vwEvents.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwEvents.View.CurrentItem).Row;
            }
        }
        #endregion

        public string KRS
        {
            get
            {
                return m_Data.Right("  " + txtEKPAUFKRS.Text.ToString().Trim(), 2);
            }
            set
            {
                txtEKPAUFKRS.Text = value;
            }
        }
        public string LFD
        {
            get
            {
                return m_Data.Right("000000" + txtEKPAUFLFD.Text.ToString().Trim(), 6); ;
            }
            set
            {
                txtEKPAUFLFD.Text = value;
            }
        }
        public string POS
        {
            get
            {
                return m_Data.Right("000000" + txtEKPAUFPOS.Text.ToString().Trim(), 3); ;
            }
            set
            {
                txtEKPAUFPOS.Text = value;
            }
        }

        BindingListCollectionView cvMaster
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgMaster.ItemsSource);
            }
        }
        BindingListCollectionView cvABA
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgABA.ItemsSource);
            }
        }
        BindingListCollectionView cvLSNAME
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgLSNAME.ItemsSource);
            }
        }
        #endregion
        public YCAEKAUDIT()
        {
            InitializeComponent();
            this.Name = "采购单管理";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            dicParamater["blBuildCellMastrix"] = false;
            base.SetUp(dicParamater);
            InitializeComponent();  
    
            m_Data = new YCAEKAUDITData();
            txtUserID.Text = (string)dicParamater["User"];
            txtPassword.Password = (string)dicParamater["Pwd"];
        }
        
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {

                if (!IsSupplierLoded)
                {
                    SetData.Merge(m_Data.LoadSupplier());
                    IsSupplierLoded = true;
                }
                if (IsLoaded)
                    return;
                IsLoaded = true;
                vwEKKOPF.View.CurrentChanged += vwEKKOPF_CurrentChanged;
                vwLIEFDAT.View.CurrentChanged += vwLIEFDAT_CurrentChanged;
                vwEKPOS.View.CurrentChanged += vwEKPOS_CurrentChanged;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;

        }
        
        void vwEKPOS_CurrentChanged(object sender, EventArgs e)
        {
            if (dwEKPOS == null)
            {
                cvABA.CustomFilter = null;
                return;
            }
            POS = dwEKPOS["EKPAUFPOS"].ToString();
            string filter = "ABAAUFKRS='" + dwEKPOS["EKPAUFKRS"].ToString() + "' AND ABAAUFLFD='" + dwEKPOS["EKPAUFLFD"].ToString() + "' AND ABAAUFPOS='" + dwEKPOS["EKPAUFPOS"].ToString() + "'";
            cvABA.CustomFilter = filter;
        }

        void vwLIEFDAT_CurrentChanged(object sender, EventArgs e)
        {
            if (dwLIEFDAT == null)
                return;
            return;
            DataSet Result = new DataSet();
            Result = m_Data.GetOrderBySupplier(dwLIEFDAT);
            SetData.EKKOPF.Clear();
            SetData.Merge(Result, false, MissingSchemaAction.Add);
        }

        void vwEKKOPF_CurrentChanged(object sender, EventArgs e)
        {
            DataSet Result = new DataSet();
            DataRow dwInput;
            dwInput = dwEKKOPF;
            if (dwInput == null)
            {
                return;
            }
            Result = m_Data.BuildData(dwInput);
            Set4ITX.Clear();
            Set4ITX.Merge(Result, false, MissingSchemaAction.Add);
        }
        public void btnQuery_Click(object sender, EventArgs e)
        {
            DataSet Result = new DataSet();
            DataRow dwInput;
            dwInput = dwEKKOPF;
            try
            {
                if (dwInput == null)
                {
                    if (LFD.Trim() == "0000000")
                        return;
                    dwInput = SetData.EKPOS.NewRow();
                    dwInput["EKPMANDANT"] = "00";
                    dwInput["EKPAUFKRS"] = KRS;
                    dwInput["EKPAUFLFD"] = LFD;
                    dwInput["EKPAUFPOS"] = POS;
                }
                RelesedQty = 0;
                Result = m_Data.BuildData(dwInput);
                Set4ITX.Clear();
                Set4ITX.Merge(Result);
                if (vwEKPOS.View != null)
                {
                    vwEKPOS.View.MoveCurrentToFirst();
                }
                if (vwEKKOPF.View != null)
                {
                    vwEKKOPF.View.MoveCurrentToFirst();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        
        private void txtLSNAME_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            try
            {
                string filter = "LSNAME1 LIKE '%" + txtLSNAME.Text.Trim() + "%'";
                cvLSNAME.CustomFilter = filter;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
 
    }
}
